if !exists(param.S)

  M291 S5 J1 F250 L150 H450 R"Set Left Tool Temperature" P"Please set the temperature for the filament previously loaded in the Left Tool."
  var ll = input
    
  M291 S5 J1 F{var.ll} L150 H450 R"Set Right Tool Temperature" P"Please set the temperature for the filament previously loaded in the Right Tool."
  var rr = input
  
  G10 P0 R{var.ll} S{var.ll}
  G10 P1 R{var.rr} S{var.rr}
  G10 P2 R{var.ll,var.rr} S{var.ll,var.rr}
  G10 P3 R{var.ll,var.rr} S{var.ll,var.rr}
  
  M568 P0 A2
  M568 P1 A2
  
  M291 S2 R"Please Remove the Build Sheet" P"To avoid damaging your machine, remove the build sheet from the build plate.<br>Click ""OK"" when you are ready to proceed or ""Cancel"" to stop." S2
  
  if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed || !move.axes[3].homed
    M98 P"0:/sys/homeall.g" S1
  
  G90
  G1 X-999 U999 Y-75 F18000
  
  M116 P0 S10
  M116 P1 S10
  
  T3
  M83                 ; relative extruder moves
  G1 E-50 F{60}*{10}   ; retract filament
  
  M98 P"0:/sys/nozzlewipe.g" C1
  M400
  while iterations < 2
    M98 P"0:/sys/nozzlewipe.g" L1 C1
    M400
  
  G1 F18000 X-50 Y-150 Z125  ; Move tools to the cleaning position
  M291 S2 R"Please Clean Both Nozzles" P"Use a metal brush to clean both nozzles thoroughly to prevent machine damage.<br>Click ""OK"" when you are ready to proceed or ""Cancel"" to stop." S2
  
  M98 P"0:/sys/homeall.g" S1
  
  G29 S2     ; disable Mesh Bed Leveling
  M290 R0 S0
  M208 Z-5 S1
  M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
  M98 P"0:/user/probeoffset.g"                       ; load global variables
  T0
  G1 U999 F18000
  M42 P4 S1                                              ; Turn on relay






























var Probe_Point_Y = 98
var max_Travel = 15
var offset = 3
var Z_Dive = 0.5

;==== T0 Z refference point ====
G90
G1 X-20 Y80 Z2 F12000
M98 P"0:/sys/nozzleprobe.g" Z1
var T0_Ref_Z = move.axes[2].userPosition

G90                                                    ; Relative
G1 Z{var.T0_Ref_Z + 1}
G1 Y{var.Probe_Point_Y} X0 F12000                                       ;Move to the Centre
G1 Z{var.T0_Ref_Z - var.Z_Dive}                        ; Move down the Bed Hole


; ===== Single Probe with T0 =====
M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
M98 P"0:/user/probeoffset.g"                       ; load global variables

; 1st Probe
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel} ; Probe LY-
M400
G4 P260
var T0_Front = move.axes[1].userPosition

G91
G1 Y{var.offset} F12000
G90

; 2nd Probe
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel} ; Probe LY+
M400
G4 P260
var T0_Rear = move.axes[1].userPosition

var T0_center_Y = {var.T0_Front + var.T0_Rear} / 2

G90
G1 Y{var.T0_center_Y} X0


; 3rd Probe
G38.4 K0 X{move.axes[0].userPosition - var.max_Travel} ; Probe LX-
M400
G4 P260
var T0_Left = move.axes[0].userPosition

G91
G1 X{var.offset} F12000
G90

; 4th Probe
G38.4 K0 X{move.axes[0].userPosition + var.max_Travel} ; Probe LX-
M400
G4 P260
var T0_Right = move.axes[0].userPosition


var T0_center_X = {var.T0_Left + var.T0_Right} / 2
G90
G1 X{var.T0_center_X} Y{var.T0_center_Y}


; ===== Final Probe with T0 =====

; 1st Probe
G90
G1 X{var.T0_center_X} Y{var.T0_Front + var.offset}
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel}
set var.T0_Front = move.axes[1].userPosition
;echo >>"0:/macros/report.txt" "T0 Y- X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

; 2nd Probe
G1 X{var.T0_center_X} Y{var.T0_Rear - var.offset}
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel}
set var.T0_Rear = move.axes[1].userPosition
;echo >>"0:/macros/report.txt" "T0 Y+ X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

set var.T0_center_Y = {var.T0_Front + var.T0_Rear} / 2

; 3rd Probe
G1 X{var.T0_Left + var.offset} Y{var.T0_center_Y}
G38.4 K0 X{move.axes[0].userPosition - var.max_Travel}
set var.T0_Left = move.axes[0].userPosition
;echo >>"0:/macros/report.txt" "T0 X- X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

; 4th Probe
G1 X{var.T0_Right - var.offset} Y{var.T0_center_Y}
G38.4 K0 X{move.axes[0].userPosition + var.max_Travel}
set var.T0_Right = move.axes[0].userPosition
;echo >>"0:/macros/report.txt" "T0 X+ X"^{move.axes[0].machinePosition}^" Y"^{move.axes[1].machinePosition}^" Z"^{move.axes[2].machinePosition}^" U"^{move.axes[3].machinePosition}

set var.T0_center_X = {var.T0_Left + var.T0_Right} / 2

G1 X{var.T0_center_X} Y{var.T0_center_Y} Z{var.T0_Ref_Z}
G4 S1
G1 Z{var.T0_Ref_Z + 5}


















M98 P"0:/sys/nozzlewipe.g" T1

;==== T1 Z refference point ====
G90
G1 X-999 U20 Y80 Z{var.T0_Ref_Z + 3} F12000
M98 P"0:/sys/nozzleprobe.g" Z10
var T1_Ref_Z = move.axes[2].userPosition

G90                                                    ; Relative
G1 Z{var.T1_Ref_Z + 1}
G1 Y100 U0 F12000                                      ;Move to the Centre
G1 Z{var.T1_Ref_Z - var.Z_Dive}                        ; Move down the Bed Hole


; ===== Single Probe with T1 =====
M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
M98 P"0:/user/probeoffset.g"                       ; load global variables

; 1st Probe
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel} ; Probe LY-
M400
G4 P260
var T1_Front = move.axes[1].userPosition

G91
G1 Y{var.offset} F12000
G90

; 2nd Probe
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel} ; Probe LY+
M400
G4 P260
var T1_Rear = move.axes[1].userPosition

var T1_center_Y = {var.T1_Front + var.T1_Rear} / 2

G90
G1 Y{var.T1_center_Y} U0


; 3rd Probe
G38.4 K0 U{move.axes[3].userPosition - var.max_Travel} ; Probe LX-
M400
G4 P260
var T1_Left = move.axes[3].userPosition

G91
G1 U{var.offset} F12000
G90

; 4th Probe
G38.4 K0 U{move.axes[3].userPosition + var.max_Travel} ; Probe LX-
M400
G4 P260
var T1_Right = move.axes[3].userPosition


var T1_center_X = {var.T1_Left + var.T1_Right} / 2
G90
G1 U{var.T1_center_X} Y{var.T1_center_Y}


; ===== Final Probe with T1 =====

; 1st Probe
G90
G1 U{var.T1_center_X} Y{var.T1_Front + var.offset}
G38.4 K0 Y{move.axes[1].userPosition - var.max_Travel}
set var.T1_Front = move.axes[1].userPosition

; 2nd Probe
G1 U{var.T1_center_X} Y{var.T1_Rear - var.offset}
G38.4 K0 Y{move.axes[1].userPosition + var.max_Travel}
set var.T1_Rear = move.axes[1].userPosition


set var.T1_center_Y = {var.T1_Front + var.T1_Rear} / 2


; 3rd Probe
G1 U{var.T1_Left + var.offset} Y{var.T1_center_Y}
G38.4 K0 U{move.axes[3].userPosition - var.max_Travel}
set var.T1_Left = move.axes[3].userPosition

; 4th Probe
G1 U{var.T1_Right - var.offset} Y{var.T1_center_Y}
G38.4 K0 U{move.axes[3].userPosition + var.max_Travel}
set var.T1_Right = move.axes[3].userPosition

set var.T1_center_X = {var.T1_Left + var.T1_Right} / 2
G1 U{var.T1_center_X} Y{var.T1_center_Y} Z{var.T1_Ref_Z}
G4 S1
G1 Z{var.T1_Ref_Z + 5}





; Calculate offsets
set global.uoffset = var.T0_center_X - var.T1_center_X
set global.yoffset = var.T0_center_Y - var.T1_center_Y


G91
G1 Z5 F12000
G90
M400

echo "Current offsets: U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}



; Generate uoffset.g
echo >"0:/user/uoffset.g" "if exists(global.uoffset)"
echo >>"0:/user/uoffset.g" "  set global.uoffset = "^{global.uoffset}
echo >>"0:/user/uoffset.g" "else"
echo >>"0:/user/uoffset.g" "  global uoffset = "^{global.uoffset}

; Generate yoffset.g
echo >"0:/user/yoffset.g" "if exists(global.yoffset)"
echo >>"0:/user/yoffset.g" "  set global.yoffset = "^{global.yoffset}
echo >>"0:/user/yoffset.g" "else"
echo >>"0:/user/yoffset.g" "  global yoffset = "^{global.yoffset}

; Generate rtzoffset.g
echo >"0:/user/rtzoffset.g" "if exists(global.rtzoffset)"
echo >>"0:/user/rtzoffset.g" "  set global.rtzoffset = "^{global.rtzoffset}
echo >>"0:/user/rtzoffset.g" "else"
echo >>"0:/user/rtzoffset.g" "  global rtzoffset = "^{global.rtzoffset}


; Generate tooloffset.g
echo >"0:/user/tooloffset.g" "                         ; Set tool offsets"
echo >>"0:/user/tooloffset.g" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}
;echo >>"0:/macros/report.txt" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}^" Z"^{global.rtzoffset}

; Apply changes
G10 P1 U{global.uoffset} Y{global.yoffset} Z{global.rtzoffset}


if !exists(param.S)
  G10 P0 S0 R0
  G10 P1 S0 R0
  G10 P2 S0 R0
  G10 P3 S0 R0
  
  M42 P4 S0                                            ; Turn off relay
  M208 Z0 S1
  M558 K0 P8 C"1.io4.in" H5 F300 T18000
  M98 P"0:/user/probeoffset.g"                         ; load global variables
  
  G90
  G1 X-999 U999 Y150 Z100 F12000


M204 T5000